#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: ct_datatype_result.py 
@version:
@time: 2022/04/11 
"""
from TapClientCore.T06_Tapdata_feature.v2.TPF05_meta.tpf_meta import TPFMeta
from TapClientCore.T07_CaseTemplate.v2.CT01_datasource.ct_datasource import CTDataSource
from TapClientCore.T07_CaseTemplate.v2.CT03_task.ct_task_base import *


class CTTaskParallelDataTypeResult(CTTaskParallel, TPFMeta, CTDataSource):
    def DATATYPE_RESULT(self, parallel_data):
        """
                全量不校验任务运行,并行执行任务
                :param parallel_data:
                :return:
                """
        data = None
        # 随机等待
        self.random_wait()
        # 并行案例初始化
        data = self.lock_and_only_get_table(parallel_data)
        record_id = data.get('id')

        operate = data.pop('operate')
        operate_source = operate.get('source')
        source_info = operate_source.get('source_info')
        source_table = operate_source.get('table')
        operate_target = operate.get('target')
        target_info = operate_target.get('target_info')
        target_table = operate_target.get('table')

        # with allure.step('通过target connection id 和目标模型名称，删除目标推演模型'):
        #     connection_id = self.get_datasource_id_by_db_info(target_info)
        #     logger.info(f'获取{target_table}的id')
        #
        #     target_meta_id = self.get_metadataInstances_id_by_source_id_and_table_name(connection_id, target_table).get(
        #         'id')
        #     logger.info(f'删除{target_table}的模型')

        #     self.delete_metadataInstances_by_id(target_meta_id)

        data['task_config']['config']['name'] += source_table
        with allure.step('创建表获取'):
            task_name = data.get('task_config').get('config').get('name')
        #
        # # connector
        # with allure.step('源数据库初始化'):
        #     _source = self.init_connector(source_info)
        # with allure.step('目标数据库初始化'):
        #     _target = self.init_connector(target_info)
        #
        # _target.tapclient_connector.CURRENT_TABLE = target_table
        #
        # source_where_column = operate_source.get('where')
        # source_delete = _source.tapclient_connector.generate_delete(
        #     source_where_column
        # )
        # # stats check
        # _stats = operate.get('stats_check').get('initial')
        # _stats_initial = _stats.get('insert')
        # self.drop_target_table(_target)
        try:
            with allure.step('创建任务'):
                record = self.create_data_copy_development(data)
            time.sleep(30)
            transfer_result = self.get_transform(record.get('create_response'))
            # from pprint import pprint
            # pprint(transfer_result)
            return transfer_result
        finally:
            ...
            # try:
            #     _source.tapclient_connector.close()
            #     _target.tapclient_connector.close()
            # except Exception as e:
            #     pass
